package com.jdk21.core.config;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class CheckLoginAspect {

//    @Resource
//    private UserLoginMapper userLoginMapper;
//
//
//    @Before("@annotation(com.lawyer.web.core.config.CheckUserLogin) && execution(public * com.lawyer.web.core.controller.*.*(..))")
//    public void checkUserLogin(JoinPoint joinPoint) throws Throwable {
//        String token = getToken(joinPoint);
//        Preconditions.checkArgument(StringUtils.isNotBlank(token), "未获取到用户身份信息");
//        UserLoginDO userLoginDO = userLoginMapper.getByToken(token);
//        Preconditions.checkArgument(userLoginDO != null, "未获取到用户身份信息");
//        updateActiveTime(userLoginDO);
//        Preconditions.checkArgument(Objects.equals(userLoginDO.getLoginStatus(), LoginStatusEnum.LOGGED_IN.getCode()), "未获取到用户身份信息");
//    }
//
//    private void updateActiveTime(UserLoginDO userLoginDO) {
//        UserLoginDO updateUserLoginDO = new UserLoginDO();
//        updateUserLoginDO.setId(userLoginDO.getId());
//        updateUserLoginDO.setActiveTime(new Date());
//        int update = userLoginMapper.updateById(updateUserLoginDO);
//        log.info("userId:{} 更新activeTime:{} update:{}", userLoginDO.getUserId(), JodaTimeUtil.format(updateUserLoginDO.getActiveTime()), update);
//    }
//
//    private String getToken(JoinPoint joinPoint) {
//        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        Preconditions.checkArgument(servletRequestAttributes != null, "未获取到用户身份信息");
//        HttpServletRequest httpServletRequest = servletRequestAttributes.getRequest();
//        /*
//        Cookie[] cookies = httpServletRequest.getCookies();
//        if (cookies == null) {
//            return "";
//        }
//        for (Cookie cookie : cookies) {
//            String cookieName = cookie.getName();
//            if (StringUtils.equals(cookieName, "token")) {
//                return cookie.getValue();
//            }
//        }
//        */
//
//        String authorization = httpServletRequest.getHeader("Authorization");
//        if (StringUtils.isNotBlank(authorization)) {
//            return authorization;
//        }
//        return httpServletRequest.getHeader("authorization");
//    }
}
